package jzbank.service.impl;

import jzbank.common.exception.SyncException;
import jzbank.dao.serializabledao.DataExportDao;
import jzbank.dao.serializabledao.TaskDao;
import jzbank.pojo.dto.DataExportDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jzbank.pojo.vo.TaskInfo;
import jzbank.service.QuartzService;
import jzbank.service.TaskService;

import java.util.List;
import java.util.UUID;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    QuartzService quartzService;

    @Autowired
    TaskDao taskDao;

    @Autowired
    DataExportDao dataExportDao;
    /**
     * 添加任务
     * @param taskInfo
     */
    @Override
    public void createJob(TaskInfo taskInfo) {
        UUID uuid = UUID.randomUUID();
        taskInfo.setTid(uuid.toString());
//        taskDao.addTaskInfo(taskInfo);
    }

    /**
     * 启动任务
     * @param id
     */
    @Override
    public void startJob(String id) {
        DataExportDTO dataExportId = dataExportDao.getDataExportId(id);
        if (dataExportId.getTid()!=null){
                quartzService.startJob(
                        dataExportId.getTaskCode(),
                        dataExportId.getTaskName(),
                        dataExportId.getCron(),
                        dataExportId.getJobGroup(),
                        dataExportId.getJobClass()
                        );
            dataExportId.setDelStatus("1");
                dataExportDao.updateDataExport(dataExportId);
        }
    }

    /**
     * 暂停任务
     * @param id
     */
    @Override
    public void pauseJob(String id){
        DataExportDTO dataExportId = dataExportDao.getDataExportId(id);
        if (dataExportId.getTid()!=null){
            if (dataExportId.getDelStatus().equals("1")){
                dataExportId.setDelStatus("0");
                dataExportDao.updateDataExport(dataExportId);
                try {
                    quartzService.pauseJob(dataExportId.getTaskCode(), dataExportId.getJobGroup());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }else {
                String info="任务未启动，不可以暂停！";
            }
        }else {
            String info="任务未找到，不可以暂停";
        }
    }

    /**
     * 恢复任务
     * @param id
     */
    @Override
    public void resumeJob(String id) {
        DataExportDTO dataExportId = dataExportDao.getDataExportId(id);
        if (dataExportId.getTid()!=null){
            if (dataExportId.getDelStatus().equals("0")){
                dataExportId.setDelStatus("1");
                dataExportDao.updateDataExport(dataExportId);
                try {
                    quartzService.resumeJob(dataExportId.getTaskCode(), dataExportId.getJobGroup());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }else {
                String info="任务未停止，不可以恢复运行！";
            }
        }else {
            String info="任务未已运行，不可以恢复";
        }
    }

    /**
     * 删除任务
     * @param id
     */
    @Override
    public void deleteJob(String id) {
        DataExportDTO dataExportId = dataExportDao.getDataExportId(id);
        if (dataExportId.getDelStatus().equals("0")){
            if (dataExportId.getTid()!=null){
                dataExportDao.deleteDataExport(id);
                try {
                    quartzService.deleteJob(dataExportId.getTaskCode(), dataExportId.getJobGroup());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }else {
                String info="任务未找到不能删除";
            }
        }else {
            String info="删除失败，任务正在运行";
        }
    }

    /**
     * 查询任务列表
     */
    @Override
    public List<TaskInfo> list() {
        List<TaskInfo> allTaskInfo = taskDao.getAllTaskInfo();
        return allTaskInfo;
    }


}
